package bb.lanxing.lib.devices.base.converter;

import android.bluetooth.BluetoothGattCharacteristic;
import bb.lanxing.lib.devices.base.CharacteristicConverter;
import bb.lanxing.lib.devices.core.ble.IGattClient;
import bb.lanxing.lib.devices.ble.BLEAttributes;
import android.util.Log;
import java.math.BigDecimal;
import java.util.UUID;

public class CrosstrainerConverter implements CharacteristicConverter {
    final static private String TAG = "CrosstrainerConverter";
    final static private boolean DebugFTMS = false;

    public void onCrosstrainer(int i, double i2, double i3, int i4, int i5, int i6,int i7 , int i8) {
    }

    public int unsignedByteToInt(byte b) {
        return b & 0xFF;
    }

    public int unsignedBytesToInt24(byte b0, byte b1, byte b2) {
        return (unsignedByteToInt(b0) + (unsignedByteToInt(b1) << 8))
                + (unsignedByteToInt(b2) << 16) ;
    }
    @Override
    public final void convert(IGattClient iGattClient, BluetoothGattCharacteristic bluetoothGattCharacteristic) {
        double doubleValueSpeed = 0;
        int intValueStepPerMinute = 0;
        int intValueStrideCount = 0;
        int intValuePower = 0;
        int intValueResistanceLevel = 0;
        int intValueEnergy = 0;
        double  doubleValueTotalDistance = 0;

        if (bluetoothGattCharacteristic.getUuid().equals(UUID.fromString(BLEAttributes.BLE_FTMS_CHAR_CROSS_TRAINER_DATA ))) {
            if(DebugFTMS){
                doubleValueSpeed = new BigDecimal((float)(bluetoothGattCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 1)*100)/360).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                doubleValueTotalDistance = new BigDecimal((float)(bluetoothGattCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 1))*1000).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                intValueStepPerMinute = (int)bluetoothGattCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 1)/2;
                intValueStrideCount = (int)bluetoothGattCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 1);
                intValueResistanceLevel = bluetoothGattCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 1)/10;
                intValuePower = bluetoothGattCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 1);
                intValueEnergy = bluetoothGattCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 1);
            }else{
                doubleValueSpeed = new BigDecimal((float)(bluetoothGattCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT16, 3))/360).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                byte[] value = bluetoothGattCharacteristic.getValue();
                doubleValueTotalDistance = new BigDecimal((float)(unsignedBytesToInt24(value[7],value[8],value[9]))).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                intValueStepPerMinute = (int)(bluetoothGattCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT16, 10))/2;
                intValueStrideCount = (int)(bluetoothGattCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT16, 14))/10;
                intValueResistanceLevel = (int)(bluetoothGattCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_SINT16, 16))/10;
                intValuePower = bluetoothGattCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_SINT16, 18);
                intValueEnergy = bluetoothGattCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT16, 22);
            }
            onCrosstrainer(iGattClient.getType(), doubleValueSpeed,doubleValueTotalDistance,intValueStepPerMinute,intValueStrideCount,intValueResistanceLevel,intValuePower,intValueEnergy);
        }
    }
}
